package com.bei_en.teacher.admin.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.bei_en.common.spring.DateEditor;
import com.bei_en.common.spring.DoubleEditor;
import com.bei_en.common.spring.IntegerEditor;
import com.bei_en.common.spring.LongEditor;
import com.bei_en.common.util.LogUtil;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;

public abstract class BaseController {

	protected Logger log = Logger.getLogger(this.getClass());
	// protected WpLogger wplog = Logger.getLogger(this.getClass());

	@Autowired
	protected HttpServletRequest request;
	
	// @Autowired
	// protected HttpServletResponse response;
	/*
	 * @Autowired private ResourceBundleMessageSource _res;
	 */

	// @Autowired
	// protected SpringContextHolder _contextHolder;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {

		binder.registerCustomEditor(int.class, new IntegerEditor());
		binder.registerCustomEditor(long.class, new LongEditor());
		binder.registerCustomEditor(double.class, new DoubleEditor());
		binder.registerCustomEditor(Date.class, new DateEditor());
	}

	/*
	 * @ExceptionHandler(Exception.class) public String exception(Exception e,
	 * HttpServletRequest request, HttpServletResponse response, String msg) {
	 * 
	 * e.printStackTrace(); log.warn(msg); request.setAttribute("exception", e);
	 * 
	 * if (ServerInfo.isAjax(request)||request.getParameter("ajax")!=null) {
	 * writeDwzSignal("300", msg, "", "", null, response);
	 * 
	 * return null; }
	 * 
	 * // ModelAndView mav = new ModelAndView("error.html"); //
	 * mav.addObject("statusCode", 300); // mav.addObject("message", msg);
	 * request.setAttribute("statusCode", 300); request.setAttribute("message",
	 * msg);
	 * 
	 * return "error"; }
	 */

	public HttpServletRequest getRequest() {

		return request;
	}

	/*
	 * public int getLoginId() {
	 * 
	 * User user = (User)
	 * request.getSession().getAttribute(Constants.AUTHENTICATION_KEY); return
	 * user!=null ? user.getId() : 0; }
	 * 
	 * public String getLoginName() {
	 * 
	 * User user = (User)
	 * request.getSession().getAttribute(Constants.AUTHENTICATION_KEY); return
	 * user!=null ? user.getUsername() : "未知"; }
	 */

	protected boolean checkLoginPhone(String phoneNum, HttpSession session) {

		LogUtil.logSessionAttr(session, log, "session");
		Object obj = session.getAttribute(Consts.LOGIN_PHONE);
		if (obj == null)
			return false;
		return phoneNum.equals(((String) obj));
	}

	protected boolean checkLoginUser(String phoneNum, HttpSession session) {

		LogUtil.logSessionAttr(session, log, "session");
		Object obj = session.getAttribute(Consts.LOGIN_USER);
		if (obj == null)
			return false;
		return phoneNum.equals(((UsersVo) obj).getMobilePhone());
	}
	protected boolean checkUser( HttpSession session) {

		LogUtil.logSessionAttr(session, log, "session");
		Object obj = session.getAttribute(Consts.LOGIN_USER);
		if (obj == null)
			return false;
		return "3".equals(((UsersVo) obj).getStatus());
	}
	public UsersVo getUser(HttpSession session){
		UsersVo obj = (UsersVo) session.getAttribute(Consts.LOGIN_USER);
	
		return obj;
	}
	
	
	public void seetingJsonObject(JSONObject object,String key ,Object value ){
		if(value==null){
			object.put(key, "");
		}else{
			object.put(key, value);
		}
	}
}
